<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="inet.util.StringTool"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="inet.db.tennis.TennisPlayer"%>
<%@page import="inet.db.tennis.TennisCountry"%>
<jsp:useBean id="tennisPlayerDAO" class="inet.db.tennis.TennisPlayerDAO" scope="session"/>
<jsp:useBean id="tennisCountryDAO" class="inet.db.tennis.TennisCountryDAO" scope="session"/>

<jsp:useBean id="req" class="inet.util.MultipartRequest">
  <jsp:setProperty name="req" property="request" value="<%=request%>" />
</jsp:useBean>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Thêm tay vợt</title>
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/nav.css" media="screen" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="css/ie6.css" media="screen" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="css/ie.css" media="screen" /><![endif]-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/fancy-button/fancy-button.css" rel="stylesheet" type="text/css" />
    <!--Jquery UI CSS-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <!-- BEGIN: load jquery -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.core.min.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.accordion.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.core.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.slide.min.js" type="text/javascript"></script>
    <!-- END: load jquery -->
    <!--jQuery Date Picker-->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.datepicker.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.progressbar.min.js" type="text/javascript"></script>
    <!--Fancy Button-->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/fancy-button/fancy-button.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/setup.js" type="text/javascript"></script>
    <!-- Load TinyMCE -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/tiny-mce/jquery.tinymce.js" type="text/javascript"></script>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $('input[type="radio"]').fancybutton();
            setupTinyMCE();
            setDatePicker('dob');
        });
    </script>
    <script type="text/javascript">
    	function f_add_player() {
    		if(document.formAddPlayer.txtName.value==''){
				document.getElementById("lblName").innerHTML = 'Nhập tên tay vợt';
				document.formAddPlayer.txtName.focus();
				return false;
			}else{
				document.getElementById("lblName").innerHTML = '';
			}
			
    		if(document.formAddPlayer.txtDob.value==''){
				document.getElementById("lblDob").innerHTML = 'Nhập ngày sinh';
				document.formAddPlayer.txtDob.focus();
				return false;
			}else{
				document.getElementById("lblDob").innerHTML = '';
			}
			
    		if(document.formAddPlayer.rdoGender.value=='0'){
				document.getElementById("lblGender").innerHTML = 'Chọn giới tính';
				document.formAddPlayer.rdoGender.focus();
				return false;
			}else{
				document.getElementById("lblGender").innerHTML = '';
			}
    		document.formAddPlayer.action.value = "CREATE";
    		document.formAddPlayer.submit();
    	}
    	function disableEnterKey(e)
		{
		     var key;
		     if(window.event)
		          key = window.event.keyCode;//IE
		     else
		          key = e.which;//firefox
		     if(key == 13)
		        f_add_player();
		}
    </script>
</head>
<body>
    <div class="container_12">
        <%@ include file="../include/header.jsp" %>
        <% 
        	// set Character Encoding before any getParameter() methods.
		    request.setCharacterEncoding("UTF-8");
		    response.setCharacterEncoding("UTF-8");
		    
		    String sAction = req.getStringParameter("action");
		    String sName = req.getStringParameter("txtName");
		    String sDob = req.getStringParameter("txtDob");
		    String sCountryId = req.getStringParameter("slbCountryId");
		    if(sCountryId == null || !StringTool.isNumberic(sCountryId))sCountryId = "0";
		    
		    String sPreperredHand = req.getStringParameter("slbPreperredHand");
		    String sMapsName = req.getStringParameter("txtMapName");
		    
		    String sWinOfYear = req.getStringParameter("txtWinOfYear");
		    if(sWinOfYear == null || !StringTool.isNumberic(sWinOfYear))sWinOfYear = "0";
		    
		    String sLoseOfYear = req.getStringParameter("txtLoseOfYear");
		    if(sLoseOfYear == null || !StringTool.isNumberic(sLoseOfYear))sLoseOfYear = "0";
		    
		    String sGender = req.getStringParameter("rdoGender");
		    if(sGender == null || !StringTool.isNumberic(sGender))sGender = "0";
		    
		    byte[] bAvatar = null;
		    long avatarLength = 0;
			
		    if (req.getFileParameter("fileAvatar") != null) {
		        BufferedInputStream is = new BufferedInputStream(req.getFileParameter("fileAvatar").getInputStream());
		        ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
		        
		        byte buffer [] = new byte[1024];
		        while (true) {
		            int iBytes = is.read(buffer);
		            // Exit the loop if there is nothing to read
		            if (iBytes == -1) {
		                break;
		            }
		            // Write the buffer to our byte array
		            baos.write(buffer, 0, iBytes);
		        }
		        bAvatar = baos.toByteArray();
		        avatarLength = bAvatar.length/1024;
		    }
		    
		    String sMessageErr = "";
		    String sMessageOk = "";
		    String sMessageWarning = "";
		    if(sAction != null && "CREATE".equals(sAction)) {
		    	if(sName == null || "".equals(sName) ) {
		    		sMessageWarning = "Bạn phải nhập tên tay vợt";
		    	} else if(sDob == null || "".equals(sDob) ) {
		    		sMessageWarning = "Bạn phải nhập ngày sinh của tay vợt";
		    	} else if(sGender == null || "".equals(sGender) ) {
		    		sMessageWarning = "Bạn phải nhập giới tính của tay vợt";
		    	} else if( bAvatar != null && avatarLength > 30) {
		    		//Check content-Length [kByte]
			        sMessageWarning = "Kích thước ảnh đại diện không được lớn hơn 30KBytes";
		    	} else {
		    		//String playerName, String dob, BigDecimal countryId , byte[] avatar, String preperredHand, int winOfYear, int loseOfYear, String mapName, int gender
		    		boolean isOk = tennisPlayerDAO.insertRow(sName, sDob, new BigDecimal(sCountryId), bAvatar, sPreperredHand, Integer.parseInt(sWinOfYear), Integer.parseInt(sLoseOfYear), sMapsName, Integer.parseInt(sGender));
		    		if(isOk) {
		    			sMessageOk = "Thêm tay vợt thành công";
		    		} else {
		    			sMessageErr = "Xảy ra lỗi khi thêm tay vợt";
		    		}
		    	}
		    }
        %>
        <div class="grid_7">
            <div class="box round first fullpage">
                <h2>Thêm tay vợt</h2>
                <div class="block ">
                    <form name="formAddPlayer" onKeyPress="return disableEnterKey(event)" method="post" enctype="multipart/form-data">
                    	<input type="hidden" name="action"/>
                    	<% if( !"".equals(sMessageWarning) ) { %>
                    	<div class="message warning">
                            <h5>Cảnh báo !</h5>
                            <p>
                                <%= sMessageWarning %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageOk) ) { %>
                    	<div class="message success">
                            <h5>Success!</h5>
                            <p>
                                <%= sMessageOk %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageErr) ) { %>
                    	<div class="message error">
                            <h5>Error!</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
	                    <table class="form">
	                        <tr>
	                            <td class="col1">
	                                <label>Tên tay vợt</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="grumble" name="txtName"/>
	                                <span id="lblName" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td class="col1">
	                                <label>Ngày sinh</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="dob" name="txtDob"/>
	                                <span id="lblDob" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td class="col1">
	                                <label>Ảnh đại diện</label>
	                            </td>
	                            <td class="col2">
	                                <input type="file" name="fileAvatar"/>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Quốc tịch</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbCountryId">
	                                	<option value="0"> - Chọn quốc tịch - </option>
	                                	<% 
	                                		Vector<TennisCountry> vTennisCountry = tennisCountryDAO.findAll();
	                                		if(vTennisCountry != null && !vTennisCountry.isEmpty()) {
	                                			TennisCountry country = null;
	                                			for (Iterator it = vTennisCountry.iterator(); it.hasNext(); ) {
	                                				country = (TennisCountry) it.next();
	                                				if(country == null) continue;
	                                	%>
	                                	<option value="<%= country.getId() %>"> <%= country.getCountryName() %></option>
	                                	<%
	                                			}
	                                    	}
	                                    %>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td class="col1">
	                                <label>Tay thuận</label>
	                            </td>
	                            <td class="col2">
	                                <select id="select" name="slbPreperredHand">
	                                	<option value=""> - Lựa chọn - </option>
	                                	<option value="Tay trái"> Tay phải </option>
	                                	<option value="Tay phải"> Tay trái </option>
	                                	<option value="Cả 2 tay"> Cả 2 tay </option>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td class="col1">
	                                <label>Số trận thắng của năm</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="grumble" name="txtWinOfYear"/>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td class="col1">
	                                <label>Số trận thua của năm</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="grumble" name="txtLoseOfYear"/>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td class="col1">
	                                <label>Maps name</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="grumble" name="txtMapName"/>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Giới tính</label>
	                            </td>
	                            <td>
	                                <input type="radio" name="rdoGender" value="0" />
	                                Nam
	                                <input type="radio" name="rdoGender" value="1" />
	                                Nữ
	                                <input type="radio" name="rdoGender" value="2" />
	                                Đôi
	                                <span id="lblGender" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                        	<td>
	                                <label>&nbsp;</label>
	                            </td>
	                            <td>
	                            	<button type="button" onclick="f_add_player();" class="btn btn-grey">Thêm mới</button>
	                            	<button type="button" onclick="window.location.href='list-player.jsp';" class="btn btn-grey">Quay lại</button>
	                            </td>
	                        </tr>
	                    </table>
                    </form>
                </div>
            </div>
        </div>
        <div class="clear">
        </div>
    </div>
    <%@ include file="../include/footer.jsp" %>
</body>
</html>